-- tenlines.lua

-- By Tyrone, on 17/04/07
-- Tells a user when they log in what the last 10 chat messages were

LastTenMessages = {}
NumberOfMessages = 0

_, botname = VH:GetConfig("config", "hub_security")

function VH_OnParsedMsgChat(nick, data)
	if (string.find(data, "^[%.%+!]")) then
		-- we start with a command
	else
		AddMessage("<"..nick.."> "..data..string.format("\n"))
	end
		
	return 1
end

function AddMessage(message)
	if (NumberOfMessages == 0) then
		LastTenMessages[1] = message
		NumberOfMessages = 1
	elseif (NumberOfMessages < 10) then
		NumberOfMessages = NumberOfMessages + 1
		LastTenMessages[NumberOfMessages] = message
	elseif (NumberOfMessages == 10) then
	 	for i = 1, 9, 1 do
			LastTenMessages[i] = LastTenMessages[i+1]
		end
		LastTenMessages[10] = message
	end
end

function GetMessages()
	if (NumberOfMessages == 0) then
		return ""
	else
		TempString = ""
		for i = 1, NumberOfMessages, 1 do
			TempString = TempString.."-- "..LastTenMessages[i] -- fixd
		end
		return TempString
	end
end

function VH_OnUserLogin(nick)
	SendMessageToUser(string.format("The last ten messages were:\n")..GetMessages(), nick, botname)
	return 1
end

function SendMessageToUser(data, nick, from)
	result, err = VH:SendDataToUser("<"..from.."> "..data.."|", nick)
	return 1
end